package com.ruoyi.web.controller.biz;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.BsQiyeConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.valid.ValidInsertGroup;
import com.ruoyi.common.valid.ValidUpdateGroup;
import com.ruoyi.system.domain.BsQiyeBusinessData;
import com.ruoyi.system.domain.ro.BsQiyeBusinessDataEditRO;
import com.ruoyi.system.domain.ro.BsQiyeBusinessDataRO;
import com.ruoyi.system.service.IBsQiyeBusinessDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.List;

/**
 * 企业经营数据
 *
 * @author ruoyi
 * @date 2023-11-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@Api(tags = "商户端-数据上报")
@RequestMapping("/system/qiyeBusinessData")
public class BsQiyeBusinessDataController extends BaseController {

    private final IBsQiyeBusinessDataService iBsQiyeBusinessDataService;

    /**
     * 查询企业经营数据列表
     */
    @ApiOperation(value = "数据上报-上报列表", tags = {"商户端-数据上报"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "0系统生成账单（未上报） 1店铺上报账单（已上报）"),
            @ApiImplicitParam(name = "qiYeId", value = "企业id"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    @GetMapping("/list")
    public Result<TableDataInfo<BsQiyeBusinessData>> list(@Pattern(regexp = "^(0)$|^(1)$", message = "type不合法") @RequestParam String type, @RequestParam Long qiYeId) {
        BsQiyeBusinessDataRO ro = BsQiyeBusinessDataRO.builder().type(type).qiyeId(qiYeId).build();
        startPage();
        List<BsQiyeBusinessData> list = iBsQiyeBusinessDataService.queryList(ro);
        return Result.success(getDataTable(list));
    }

    /**
     * 获取企业经营数据详细信息
     *
     * @param id 主键
     */
    @ApiOperation(value = "数据上报-详情", tags = {"商户端-数据上报"})
    @GetMapping("/{id}")
    public Result<BsQiyeBusinessData> getInfo(@NotNull(message = "主键不能为空")
                                              @PathVariable Long id) {
        return Result.success(iBsQiyeBusinessDataService.queryById(id));
    }

    /**
     * 新增企业经营数据-暂时用不到
     */
    @Log(title = "企业经营数据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public Result<?> add(@Validated(ValidInsertGroup.class) @RequestBody BsQiyeBusinessDataRO ro) {
        ro.setType(BsQiyeConstants.REPORT_TYPE.ALREADY_REPORT);
        return Result.toResult(iBsQiyeBusinessDataService.insertByBo(ro));
    }

    /**
     * 修改企业经营数据
     */
    @ApiOperation(value = "数据上报-修改", tags = {"商户端-数据上报"})
    @Log(title = "企业经营数据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public Result<?> edit(@Validated(ValidUpdateGroup.class) @RequestBody BsQiyeBusinessDataEditRO ro) {
        ro.setType(BsQiyeConstants.REPORT_TYPE.ALREADY_REPORT);
        return Result.toResult(iBsQiyeBusinessDataService.updateByBo(ro));
    }
}
